#!/bin/bash

# Vision Pro AI 对话服务启动脚本

# 解析命令行参数
HTTPS_FRONTEND=false
HTTPS_BACKEND=false
FRONTEND_PORT=8080
BACKEND_PORT=9001

while [[ $# -gt 0 ]]; do
    case $1 in
        --https-frontend)
            HTTPS_FRONTEND=true
            shift
            ;;
        --https-backend)
            HTTPS_BACKEND=true
            shift
            ;;
        --https-full)
            HTTPS_FRONTEND=true
            HTTPS_BACKEND=true
            shift
            ;;
        --frontend-port)
            FRONTEND_PORT="$2"
            shift 2
            ;;
        --backend-port)
            BACKEND_PORT="$2"
            shift 2
            ;;
        -h|--help)
            echo "使用方法: $0 [选项]"
            echo "选项:"
            echo "  --https-frontend     启动HTTPS前端服务器（支持WebXR）"
            echo "  --https-backend      启动HTTPS后端服务器"
            echo "  --https-full         同时启动HTTPS前端和后端服务器"
            echo "  --frontend-port PORT 前端服务器端口 (默认: 8080)"
            echo "  --backend-port PORT  后端服务器端口 (默认: 8000)"
            echo "  -h, --help          显示此帮助信息"
            echo ""
            echo "示例:"
            echo "  $0                   启动HTTP后端服务器"
            echo "  $0 --https-frontend  启动HTTPS前端服务器"
            echo "  $0 --https-backend   启动HTTPS后端服务器"
            echo "  $0 --https-full      启动完整HTTPS服务（推荐）"
            exit 0
            ;;
        *)
            echo "未知选项: $1"
            echo "使用 $0 --help 查看帮助"
            exit 1
            ;;
    esac
done

if [ "$HTTPS_FRONTEND" = true ] && [ "$HTTPS_BACKEND" = true ]; then
    echo "🔒 启动完整 HTTPS 服务 (前端+后端，支持WebXR)..."
elif [ "$HTTPS_FRONTEND" = true ]; then
    echo "🔒 启动 HTTPS 前端服务器 (支持WebXR)..."
elif [ "$HTTPS_BACKEND" = true ]; then
    echo "🔒 启动 HTTPS 后端服务器..."
else
    echo "🚀 启动 Vision Pro AI 对话后端服务..."
fi

# 检查 Python 版本
python_version=$(python3 --version 2>&1)
echo "📋 Python 版本: $python_version"

# 检查是否存在虚拟环境
if [ -d "venv" ]; then
    echo "🔄 激活虚拟环境..."
    source venv/bin/activate
fi

# 检查依赖是否安装
if [ ! -f "requirements.txt" ]; then
    echo "❌ 未找到 requirements.txt 文件"
    exit 1
fi

echo "📦 检查依赖..."
pip install -r requirements.txt

# 检查环境变量文件
if [ ! -f ".env" ]; then
    echo "⚠️  未找到 .env 文件，请复制 .env.example 并配置："
    echo "   cp .env.example .env"
    echo "   然后编辑 .env 文件，填入你的 DASHSCOPE_API_KEY"
    exit 1
fi

# 创建音频文件目录
mkdir -p audio_files
echo "📁 音频文件目录已准备"

# 启动服务
if [ "$HTTPS_FRONTEND" = true ] && [ "$HTTPS_BACKEND" = true ]; then
    echo "🌟 启动完整HTTPS服务..."
    echo "🔒 前端地址: https://localhost:$FRONTEND_PORT"
    echo "🔒 后端地址: https://localhost:$BACKEND_PORT"
    echo "🌐 网络访问: https://$(python3 -c "import socket; s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM); s.connect(('8.8.8.8', 80)); print(s.getsockname()[0]); s.close()"):$FRONTEND_PORT"
    echo "🥽 WebXR支持: ✅ 已启用"
    echo "🔗 前后端连接: ✅ 安全连接"
    echo ""
    echo "按 Ctrl+C 停止服务"
    echo "=========================="
    
    # 启动后端服务器（后台运行）
    echo "🚀 启动HTTPS后端服务器..."
    python3 https_backend.py --port $BACKEND_PORT &
    BACKEND_PID=$!
    
    # 等待后端启动
    sleep 3
    
    # 启动前端服务器
    echo "🚀 启动HTTPS前端服务器..."
    cd frontend
    python3 https_server.py --port $FRONTEND_PORT
    
    # 清理后台进程
    kill $BACKEND_PID 2>/dev/null
    
elif [ "$HTTPS_FRONTEND" = true ]; then
    echo "🌟 启动HTTPS前端服务器..."
    echo "🔒 HTTPS地址: https://localhost:$FRONTEND_PORT"
    echo "🌐 网络访问: https://$(python3 -c "import socket; s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM); s.connect(('8.8.8.8', 80)); print(s.getsockname()[0]); s.close()"):$FRONTEND_PORT"
    echo "🥽 WebXR支持: ✅ 已启用"
    echo "📁 服务目录: frontend/"
    echo "⚠️  注意: 需要单独启动后端服务器"
    echo ""
    echo "按 Ctrl+C 停止服务"
    echo "=========================="
    
    cd frontend
    python3 https_server.py --port $FRONTEND_PORT
    
elif [ "$HTTPS_BACKEND" = true ]; then
    echo "🌟 启动HTTPS后端服务器..."
    echo "🔒 HTTPS地址: https://localhost:$BACKEND_PORT"
    echo "🌐 网络访问: https://$(python3 -c "import socket; s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM); s.connect(('8.8.8.8', 80)); print(s.getsockname()[0]); s.close()"):$BACKEND_PORT"
    echo "📖 API 文档: https://localhost:$BACKEND_PORT/docs"
    echo "🔍 健康检查: https://localhost:$BACKEND_PORT/health"
    echo ""
    echo "💡 提示: 使用 --https-full 启动完整HTTPS服务"
    echo ""
    echo "按 Ctrl+C 停止服务"
    echo "=========================="
    
    python3 https_backend.py --port $BACKEND_PORT
    
else
    echo "🌟 启动HTTP后端服务..."
    echo "📍 服务地址: http://localhost:$BACKEND_PORT"
    echo "📖 API 文档: http://localhost:$BACKEND_PORT/docs"
    echo "🔍 健康检查: http://localhost:$BACKEND_PORT/health"
    echo ""
    echo "💡 提示: 使用 --https-full 启动完整HTTPS服务以支持WebXR"
    echo ""
    echo "按 Ctrl+C 停止服务"
    echo "=========================="
    
    python main.py
fi